﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Dapper;
using DapperExtensions;
using MaximServices.Data;
using MaximServices.Model;
using Microsoft.Extensions.Options;
using MySql.Data.MySqlClient;

namespace MaximServices.Repository
{
    public class ViewHistoryRepository
    {
        private readonly DataBaseRepository _dbrsp;

        public ViewHistoryRepository(DataBaseRepository dbrsp)
        {
            _dbrsp = dbrsp;
        }

        public void Dispose()
        {
            _dbrsp.Database.Dispose();
        }

        public ViewHistory GetModelByNow(DateTime date, string userId)
        {
            var pgMain = new PredicateGroup { Operator = GroupOperator.And, Predicates = new List<IPredicate>() };

            pgMain.Predicates.Add(Predicates.Field<ViewHistory>(f => f.View_Date, Operator.Eq, date.ToString("yyyy-MM-dd")));
            pgMain.Predicates.Add(Predicates.Field<ViewHistory>(f => f.User_Id, Operator.Eq, userId));

            return _dbrsp.Database.GetList<ViewHistory>(pgMain, null, null, false).FirstOrDefault();
        }
        public List<ViewHistory> GetHistoryList(DateTime start, DateTime end, string userId)
        {
            var pgMain = new PredicateGroup { Operator = GroupOperator.And, Predicates = new List<IPredicate>() };
            pgMain.Predicates.Add(Predicates.Field<ViewHistory>(f => f.View_Date, Operator.Ge, start.ToString("yyyy-MM-dd")));
            pgMain.Predicates.Add(Predicates.Field<ViewHistory>(f => f.View_Date, Operator.Le, end.ToString("yyyy-MM-dd")));
            pgMain.Predicates.Add(Predicates.Field<ViewHistory>(f => f.User_Id, Operator.Eq, userId));

            return _dbrsp.Database.GetList<ViewHistory>(pgMain, null, null, false).ToList();
        }

        public bool AddHistory(ViewHistory model)
        {
            try
            {
                _dbrsp.Database.Insert(model,null);
                return true;
            }
            catch (Exception)
            {
                throw;
            }
        }

    }
}
